package com.ybkj.daijia.server.event.listener;


import com.ybkj.daijia.serverUtils.SettingUtils;
import com.ybkj.daijia.server.event.model.DriverUpdateLocationEvent;
import com.ybkj.daijia.server.model.Company;
import com.ybkj.daijia.server.model.Driver;
import com.ybkj.daijia.server.service.CompanyService;
import com.ybkj.daijia.server.service.DriverService;
import com.ybkj.daijia.server.service.SmsTempletService;
import com.ybkj.daijia.server.websocket.WebSocketSend;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

@Component
public class DriverUpdateLocationEventListener implements
    ApplicationListener<DriverUpdateLocationEvent> {

    private static final Logger logger = LoggerFactory
        .getLogger(DriverUpdateLocationEventListener.class);

    @Autowired
    private SmsTempletService smsTempletService;

    @Autowired
    private DriverService driverService;

    @Autowired
    private SettingUtils settingUtils;

    @Autowired
    private CompanyService companyService;


    @Override
    @Async
    public void onApplicationEvent(DriverUpdateLocationEvent event) {

        logger.debug("driver UpdateLocation event:{}", "");

        Driver driver = (Driver) event.getSource();

        //推送
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("driverID", driver.getId());
        jsonObject.put("latitude", event.getLat());
        jsonObject.put("longitude", event.getLng());
        jsonObject.put("address", event.getAddr());
        jsonObject.put("type", "updateUserLocation");
        Company mainCompany = companyService.getMain();
        Company driverCompany = companyService.findOne(driver.getCompanyId());
        WebSocketSend.boardcastToArea(jsonObject, driverCompany.getCityName(), mainCompany.getId());

    }

}
